<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/24
 * Time: 18:48
 */

namespace App\Game;

use App\Models\GameList;
use Carbon\Carbon;
use App\Game\Traits\BbinTrait;

class bbin extends Game
{
    use BbinTrait;

    public $timezone = 'GMT-4';
    private $platName = 'bbin';
    private $gameType = [
        1 => 'sport',
        3 => 'live',
        5 => 'e-game',
        12 => 'lottery',
        30 => 'e-game',
        38 => 'e-game',
    ];


    public function __construct()
    {
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
        ];

    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        //$actype = $request->demo ? 0 : 1;
        $action = [
            'command' => 'createmember',
            'parameters' => [
                'username' => $accountUserName,
                'password' => $password,
            ]
        ];

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = json_decode($output['result'], true);
                if ($result['result']) {
                    return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $result['data']['Message']];
                }
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $action = [
            'command' => 'checkusrbalance',
            'parameters' => [
                'username' => $accountUserName,
            ]
        ];

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = json_decode($output['result'], true);
                return ['success' => true, 'data' => ['balance' => $result['TotalBalance']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $action = [
            'command' => 'transfer',
            'parameters' => [
                'username' => $accountUserName,
                'remitno' => $transferid,
                'action' => 'IN',
                'remit' => $price,
            ]
        ];

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = json_decode($output['result'], true);
                if ($result['result']) {
                    return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转入成功，转账失败原因：' . $result['data']['Message']];
                }
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转入失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $action = [
            'command' => 'transfer',
            'parameters' => [
                'username' => $accountUserName,
                'remitno' => $transferid,
                'action' => 'OUT',
                'remit' => $price,
            ]
        ];

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = json_decode($output['result'], true);
                if ($result['result']) {
                    return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转出成功，转账失败原因：' . $result['data']['Message']];
                }
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转出失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $action = [
            'command' => 'checktransfer',
            'parameters' => [
                'transid' => $request->transferid,
            ]
        ];

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = json_decode($output['result'], true);
                if ($result['result']['data']['Status']) {
                    return ['success' => true, 'data' => ['transfer_state'=>1], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => ['transfer_state'=>0], 'message' => '转账失败，查询失败原因：' . $result['message']];
                }
            } else {
                return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '查询订单失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick($request)
    {
        $action = [
            'command' => 'logout',
            'parameters' => [
                'username' => $request->accountUserName,
            ]
        ];

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = json_decode($output['result'], true);
                if ($result['result']) {
                    return ['success' => true, 'data' => [], 'message' => ''];
                } else {
                    return ['success' => false, 'data' => [], 'message' => '玩家踢线失败，原因：' . $result['data']['Message']];
                }
            } else {
                return ['success' => false, 'data' => [], 'message' => '玩家踢线失败，原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $game = GameList::where('main_game_plat_code', $this->platName)
            ->where('pc_game_code', $request->gamecode)
            ->first();
        if (in_array($request->gamecode, ['ball', 'live', 'game', 'Ltlottery'])) {
            $action = [
                'command' => 'getloginurl',
                'parameters' => [
                    'username' => $request->accountUserName,
                    'page_site' => $request->gamecode,
                    'lang' => 'zh-cn',
                ]
            ];
        } else {
            if (empty($game)) {
                $action = [
                    'command' => 'getloginurl',
                    'parameters' => [
                        'username' => $request->accountUserName,
                        'lang' => 'zh-cn',
                    ]
                ];
            } else {
                $action = [
                    'command' => 'getgamelink',
                    'parameters' => [
                        'username' => $request->accountUserName,
                        'gamekind' => $game->pc_game_deputy_code,
                        'gametype' => $request->gamecode,
                        'gamecode' => $game->pc_game_deputy_code == 3 ? 1 : '',
                        'lang' => 'zh-cn',
                    ]
                ];
            }

        }

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                return ['success' => true, 'data' => ['url' => $output['result']], 'message' => ''];

            } else {
                return ['false' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['result']];
            }
        } else {
            return ['false' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $game = GameList::where('main_game_plat_code', $this->platName)
            ->where('pc_game_code', $request->gamecode)
            ->first();

        if (in_array($request->gamecode, ['ball', 'live', 'game', 'Ltlottery'])) {
            $action = [
                'command' => 'getloginurl',
                'parameters' => [
                    'username' => $request->accountUserName,
                    'page_site' => $request->gamecode,
                    'lang' => 'zh-cn',
                ]
            ];
        } else {
            if (empty($game)) {
                $action = [
                    'command' => 'getloginurl',
                    'parameters' => [
                        'username' => $request->accountUserName,
                        'lang' => 'zh-cn',
                    ]
                ];
            } else {
                if($game->pc_game_deputy_code==3){
                    $action = [
                        'command' => 'getgamelink',
                        'parameters' => [
                            'username' => $request->accountUserName,
                            'gamekind' => $game->pc_game_deputy_code,
                            'gametype' => $request->gamecode,
                            'gamecode' => $game->pc_game_deputy_code == 3 ? 1 : '',
                            'lang' => 'zh-cn',
                        ]
                    ];
                }elseif(in_array($game->pc_game_deputy_code,[5,30,38])){
                    $action = [
                        'command' => 'geth5slotslink',
                        'parameters' => [
                            'username' => $request->accountUserName,
                            'gametype' => $request->gamecode,
                            'lang' => 'zh-cn',
                        ]
                    ];
                }else{
                    $action = [
                        'command' => 'getloginurl',
                        'parameters' => [
                            'username' => $request->accountUserName,
                            'lang' => 'zh-cn',
                        ]
                    ];
                }
            }
        }

        $output = $this->byRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                return ['success' => true, 'data' => ['url' => $output['result']], 'message' => ''];

            } else {
                return ['false' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['result']];
            }
        } else {
            return ['false' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $pageSize = 1000;
        $start_time = $startTime->toDateTimeString();
        $end_time = $endTime->toDateTimeString();

        foreach ($this->types as $type) {
            $pageNumber = 1;
            do {
                $flag = false;
                $action = [
                    'command' => 'getrawbethistory',
                    'parameters' => [
                        'startDate' => $start_time,
                        'endDate' => $end_time,
                        'type' => $type,
                        'pageNumber' => $pageNumber,
                        'pageSize' => $pageSize,
                    ]
                ];

                $output = $this->byRequest($action);
                $output = json_decode($output, true);
                if (array_key_exists('result', $output)) {
                    if ($output['status'] == 200) {
                        $result = json_decode($output['result'], true);
                        if (count($result['betHistories']) >= 1) {
                            $items = [];
                            $items = array_merge($items, $result['betHistories']);
                            //添加平台类型数据
                            array_walk($items, function (&$value) use ($type) {
                                $value = json_decode($value, true);
                                $value['platformType'] = $type;
                            }
                            );
                            $data = array_merge($data, $items);
                            $flag = true;
                        }

                    }
                }
                $pageNumber++;
            } while ($flag);
        }

        return $data;
    }
}